package com.ysg.widget.image;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import com.ysg.R;
import com.ysg.utils.YDisplayUtil;

public class TrapezoidImageView extends androidx.appcompat.widget.AppCompatImageView {
    private int mWidth = 0;
    private int mHeight = 0;
    private float incline;
    private float radius;

    public TrapezoidImageView(Context context) {
        super(context);
    }

    public TrapezoidImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TrapezoidImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TrapezoidImageView);
        incline = a.getDimension(R.styleable.TrapezoidImageView_trapezoidIncline, YDisplayUtil.dp2px(getContext(), 20f));
        radius = a.getDimension(R.styleable.TrapezoidImageView_trapezoidRadius, YDisplayUtil.dp2px(getContext(), 10f));
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(getResources().getColor(R.color.transparent));


        Path path = new Path();
        int width = mWidth;
        int height = mHeight;

        path.moveTo(radius, 0);
        //右上
        path.lineTo(width - radius, 0);
        path.quadTo(width, 0, width, radius);
        //右下
        path.lineTo(width - (incline/2), height - (incline/2));
        path.quadTo(width - (incline/2), height, width - incline, height);
        //左下
        path.lineTo(radius, height);
        path.quadTo(0, height, 0, height - radius);
        //左上
        path.lineTo(0, radius);
        path.quadTo(0, 0, radius, 0);
        path.close(); // 使这些点构成封闭的多边形

        canvas.clipPath(path);
        canvas.drawPath(path, paint);
        super.onDraw(canvas);
    }



}
